package com.sy.socket.test;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SocketService {
	public static final int PORT = 8000;
	public static final String PATH = "D://test/upload/";
	Map<String,String> map = new HashMap<>();

	public void start() {
		System.out.println("start...");
		try ( // 创建服务器端对象
			ServerSocket server = new ServerSocket(PORT);) {
			while (true) {
				Socket socket = server.accept();
					DataInputStream inputStream = new DataInputStream(socket.getInputStream());
					byte[] bytes = new byte[1];//用于存放字节数
			        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
			        boolean start = false;
			        int index = 0;//用户获取值
			        boolean binaryfile = false;
			        while (-1 != inputStream.read(bytes)) {//页面请求后，最后会一致阻塞，知道页面刷新。即关闭连接。才会返回-1
			            byteArray.write(bytes);
			            if (bytes[0] == '\n') {//先检测读取的时换行结尾的
			            	String context = byteArray.toString();
			            	if (context.equals("\r\n")) {//如果只是回车换行，则跳过
			            		byteArray.reset();//重置
			            		continue;
			            	}else if (context.endsWith("\r\n")) {
			                	if(binaryfile) {
			                		FileOutputStream out = new FileOutputStream(PATH + "1.rar");
			                		
			                		byte[] byteArray2 = byteArray.toByteArray();
			                		out.write(byteArray2, 0, byteArray2.length-2);//将最后的/r/n去掉
			                		out.flush();
			                		out.close();
			                		
			                		binaryfile = false;
			                	}else {
			                		System.out.println("内容为:"+context);
			                	}
			                	
			                	byteArray.reset();
			                	
			                	if(start) {
			                		index++;
			                	}
			                	if(index == 1) {
			                		if (context.startsWith("Content-Type: application/octet-stream")) { 
			                			binaryfile = true;
			                		}
			                	}else if(index == 2) {
			                		start = false;
			                		index = 0;
			                	}
			                	
			                	
			                	if(context.startsWith("Content-Disposition: form-data;")){
			                		String name = context.split(";")[1].split("=")[1];
			                		start = true;
			                	}
			                }
			                
			                	
			                
			            }
			            
			        }
			        OutputStream outputStream = socket.getOutputStream();
			        outputStream.write("客户端,我收到信息了".getBytes("UTF-8"));
			        outputStream.flush();
			        inputStream.close();
			        outputStream.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new SocketService().start();
	}

}